import com.sun.source.tree.NewArrayTree;

import java.util.*;

public class BinaryTree {
    static class TreeNode {


        public int val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(int val) {
            this.val = val;
        }
    }

    public TreeNode root;//将来这个引用 指向的就是根节点

    public TreeNode creatTree() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');
        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        E.right = H;
        return A;

    }

    // 前序遍历
    public void preOrder(TreeNode root) {
        if (root == null) return;
        System.out.print(root.val + " ");
        preOrder(root.left);
        preOrder(root.right);
    }


    // 中序遍历
    void inOrder(TreeNode root) {
        if (root == null) return;
        inOrder(root.left);
        System.out.print(root.val + " ");
        inOrder(root.right);
    }

    // 后序遍历
    void postOrder(TreeNode root) {
        if (root == null) return;
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val + " ");
    }


    /*
    将前序遍历的结果 存储到list当中
     */
      /* List<TreeNode> ret = new ArrayList<>();
    public List<TreeNode> preOrder2(TreeNode root) {
        if(root == null) return ret;
        //System.out.print(root.val+" ");
        ret.add(root);
        preOrder2(root.left);
        preOrder2(root.right);
        return ret;
    }*/
    public List<TreeNode> preOrder2(TreeNode root) {
        List<TreeNode> ret = new ArrayList<>();
        if (root == null) return ret;
        ret.add(root);
        List<TreeNode> leftTree = preOrder2(root.left);
        ret.addAll(leftTree);//将leftTree添加到ret中
        List<TreeNode> rightTree = preOrder2(root.right);
        ret.addAll(rightTree);
        return ret;
    }

    //中序遍历
    public List<TreeNode> inOrder2(TreeNode root) {
        List<TreeNode> ret = new ArrayList<>();
        if (root == null) return ret;

        List<TreeNode> leftTree = inOrder2(root.left);
        ret.addAll(leftTree);

        ret.add(root);

        List<TreeNode> rightTree = inOrder2(root.right);
        ret.addAll(rightTree);

        return ret;
    }


    //后序遍历
    public List<TreeNode> postOrder2(TreeNode root) {
        List<TreeNode> ret = new ArrayList<>();
        if (root == null) return ret;

        List<TreeNode> leftTree = postOrder2(root.left);
        ret.addAll(leftTree);


        List<TreeNode> rightTree = postOrder2(root.right);
        ret.addAll(rightTree);

        ret.add(root);

        return ret;
    }


    //获取树中节点的个数：遍历思路
    public int size = 0;

    //前序遍历的方式
    public int nodeSize(TreeNode root) {
        if (root == null) return 0;
        size++;
        nodeSize(root.left);
        nodeSize(root.right);
        return size;
    }

    /**
     * 获取节点的个数：子问题的思路
     */
    public int nodeSize2(TreeNode root) {
        if (root == null) return 0;

        return nodeSize2(root.left) + nodeSize2(root.right) + 1;
    }


    /*
     获取叶子节点的个数：遍历思路
     */
    public static int leafSize = 0;

    public void leafSize(TreeNode root) {
        if (root == null) return;
        if (root.left == null && root.right == null) {
            leafSize++;
        }
        leafSize(root.left);
        leafSize(root.right);
    }

    //获取叶节点个数 子问题思路
    //root左树的叶子+root右树的叶子
    public int getLeafNodeCount1(TreeNode root) {
        if (root == null) return 0;
        if (root.left == null && root.right == null) {
            return 1;
        }
        return getLeafNodeCount1(root.left) + getLeafNodeCount1(root.right);
    }


    /*
    获取第K层节点的个数  子问题
     */
    int getKLevelNodeCount(TreeNode root, int k) {
        if (root == null) return 0;
        if (k == 1) {
            return 1;
        }
        return getKLevelNodeCount(root.left, k - 1) +
                getKLevelNodeCount(root.right, k - 1);
    }

    /*
     获取二叉树的高度
     时间复杂度：O(N)
     */
    int getHeight(TreeNode root) {
        if (root == null) return 0;
        int leftHeight = getHeight(root.left);
        int rightHeight = getHeight(root.right);

        return (leftHeight > rightHeight ?
                leftHeight + 1 : rightHeight + 1);
    }


    // 检测值为value的元素是否存在
    public boolean find(TreeNode root, char val) {
        if (root == null) return false;
        if (root.val == val) {
            return true;
        }
        boolean leftVal = find(root, val);
        if (leftVal == true) {
            return true;
        }
        boolean rightVal = find(root, val);
        if (rightVal == true) {
            return true;
        }
        return false;
    }

    //层序遍历
    public void levelOrder1(TreeNode root) {
        if (root == null) {
            return;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            TreeNode cur = queue.poll();
            System.out.print(cur.val + " ");
            if (cur.left != null) {
                queue.offer(cur.left);
            }
            if (cur.right != null) {
                queue.offer(cur.right);
            }
        }
    }
    //层序遍历 即逐层地，从左到右访问所有节点
    public List<List<TreeNode>> levelOrder(TreeNode root) {
        List<List<TreeNode>> ret = new ArrayList<>();
        if (root == null) {
            return ret;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            //求队列大小
            int size = queue.size();
            List<TreeNode> tmp = new ArrayList<>();
            while (size != 0) {
                TreeNode cur = queue.poll();
                tmp.add(cur);
                size--;
                if (cur.left != null) {
                    queue.offer(cur.left);
                }
                if (cur.right != null) {
                    queue.offer(cur.right);
                }
            }
            ret.add(tmp);
        }
        return ret;
    }
    //层序遍历 即逐层地，从左到右访问所有节点
    class Solution {

        public List<List<Integer>> levelOrder (TreeNode root){
            List<List<Integer>> list = new ArrayList<>();
            if(root == null){
                return list;
            }
            Queue<TreeNode> queue = new LinkedList<>();
            queue.offer(root);
            while(!queue.isEmpty()){
                int size = queue.size();
                List<Integer> tmp = new ArrayList<>();
                while(size > 0){
                    TreeNode cur = queue.poll();
                    tmp.add(cur.val);
                    size--;
                    if(cur.left != null){
                        queue.offer(cur.left);
                    }
                    if(cur.right != null){
                        queue.offer(cur.right);
                    }
                }
                list.add(0,tmp);
            }
            return list;
        }
    }

    //给你二叉树的根节点 root ，返回其节点值 自底向上的层序遍历
    // 即按从叶子节点所在层到根节点所在的层，逐层从左向右遍历）
    public List<List<Integer>> levelOrderBottom(TreeNode root) {
        List<List<Integer>> list = new ArrayList<>();
        if(root == null){
            return list;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()){
            int size = queue.size();
            List<Integer> tmp = new ArrayList<>();
            while(size > 0){
                TreeNode cur = queue.poll();
                tmp.add(cur.val);
                size--;
                if(cur.left != null){
                    queue.offer(cur.left);
                }
                if(cur.right != null){
                    queue.offer(cur.right);
                }
            }
            list.add(0,tmp);
        }
        return list;

    }




    // 判断一棵树是不是完全二叉树
    //null后只能为null 则为完全二叉树 null后还有值则不是
    boolean isCompleteTree(TreeNode root) {
        if(root == null){
            return true;
        }
        Queue<TreeNode> queue= new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()){
            TreeNode cur = queue.poll();
            if(cur != null){
                queue.offer(cur.left);
                queue.offer(cur.right);
            }else{
                //遇到null了
                break;
            }
        }
        //弹出剩下的null
        while(! queue.isEmpty()){
            TreeNode cur = queue.poll();
            if(cur != null){
                return false;
            }
        }
        return true;
    }


    //两个指定节点的最近公共祖先
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if(root == null){
            return null;
        }
        if(root == p || root == q){
            return root;
        }
        TreeNode leftTree = lowestCommonAncestor(root.left,p,q);
        TreeNode rightTree = lowestCommonAncestor(root.right,p,q);
        // 如果在左子树和右子树中都找到了 p 和 q 的可能祖先，那么根节点就是它们的最低公共祖先
        if (leftTree!= null && rightTree!= null) {
            return root;
        } else if (leftTree!= null) {
            // 如果仅在左子树中找到了，返回左子树的结果
            return leftTree;
        } else {
            // 否则返回右子树的结果
            return rightTree;
        }
    }

    //另一个方法 获取根节点到两个节点的路径
    private boolean getPath(TreeNode root, TreeNode node, Stack<TreeNode> stack) {
        if(root == null || node == null) {
            return false;
        }
        stack.push(root);
        if(root == node) {
            return true;
        }
        boolean flg = getPath(root.left,node,stack);
        if(flg) {
            return true;
        }
        boolean flg2 = getPath(root.right,node,stack);
        if(flg2) {
            return true;
        }
        stack.pop();
        return false;
    }

    //链表相交思想  相交的点即为公共父节点
    public TreeNode lowestCommonAncestor2(TreeNode root, TreeNode p, TreeNode q) {
        if(root == null) {
            return null;
        }
        Stack<TreeNode> stackP = new Stack<>();
        Stack<TreeNode> stackQ = new Stack<>();

        getPath(root,p,stackP);
        getPath(root,q,stackQ);

        //对栈的操作
        int sizeP = stackP.size();
        int sizeQ = stackQ.size();

        if(sizeP > sizeQ) {
            int size = sizeP - sizeQ;
            while (size != 0) {
                stackP.pop();
                size--;
            }
        }else {
            int size = sizeQ - sizeP;
            while (size != 0) {
                stackQ.pop();
                size--;
            }
        }
        //两个栈当中 元素的个数是相同的
        while (!stackP.isEmpty() && !stackQ.isEmpty()) {
            if(stackP.peek() .equals(stackQ.peek())) {
                return stackP.peek();
            }
            stackP.pop();
            stackQ.pop();
        }
        return null;
    }

    //判断两棵树是不是相同的树 前序遍历
    public boolean isSameTree(TreeNode p,TreeNode q){
        if(p == null && q != null || p != null && q == null ){
            return false;
        }
        if(p == null && q == null){ //都走到了null这一步
                                   // 说明在判断值是否相等时已经过关返回true
            return true;
        }
        if(p.val != q.val){
            return false;
        }
        return isSameTree(p.left,q.left) && isSameTree(p.right,q.right);
    }

    //是否是另一棵树的子树
    public boolean isSubtree(TreeNode root,TreeNode subRoot){
        if (root ==null) return false;
        if(isSameTree(root,subRoot)){
            return true;
        }
        if(isSubtree(root.left,subRoot)){
            return true;
        }
        if(isSubtree(root.right,subRoot)){
            return true;
        }
        return false;

    }

    //翻转二叉树
    public TreeNode invertTree(TreeNode root){
        if(root == null){
            return null;
        }
        TreeNode tmp = root.left;
        root.left = root.right;
        root.right = tmp;
        invertTree(root.left);
        invertTree(root.right);
        return root;
    }

    //判断是否是平衡二叉树
    //一个二叉树每个节点的左右两个子树的高度差的绝对值不超过一
    public int getHeight1(TreeNode root){
        if(root == null)return 0;
        int leftHeight = getHeight1(root.left);
        int  rightHeight = getHeight1(root.right);
        if(leftHeight >= 0 && rightHeight >= 0 && Math.abs(leftHeight - rightHeight) <= 1){
            return Math.max(leftHeight,rightHeight)+1;
        }else {
            return -1;
        }
    }
    public boolean isbalencedTree(TreeNode root){
        if(root == null){
            return true;
        }
        return getHeight(root)>=0;
    }


    //是否是对称二叉树
    public boolean isSymmetric(TreeNode root){
        if(root == null){
            return true;
        }
            return isSymmetricChild(root.left,root.right);
    }
    public boolean isSymmetricChild(TreeNode p,TreeNode q){
        if(p == null && q == null){
            return true;
        }
        if(p == null && q != null || p != null && q == null){
            return false;
        }
        if(p.val != q.val){
            return false;
        }
        return isSymmetricChild(p.left,q.right)&& isSymmetricChild(p.right,q.left);
    }




    //二叉树的构建及遍历
    //读入先序遍历的字符串 建立次二叉树 建立后在进行中序遍历

     public static class TreeNode1{
        public char val;
        public TreeNode1 left;
        public TreeNode1 right;

        public TreeNode1(char val) {
            this.val = val;
        }
    }



    public class Main {
        public static int i = 0;
        public static TreeNode1 createTree(String str){
            TreeNode1 root = null;

            if (str.charAt(i) != '#') {
                root = new TreeNode1(str.charAt(i));
                i++;
                root.left = createTree(str);
                root.right = createTree(str);
            } else {
                i++;
            }
            return root;
        }

        public static void inOrderTraversal(TreeNode1 root){
            if (root == null) {
                return;
            }
            inOrderTraversal(root.left);
            System.out.print(root.val+" ");
            inOrderTraversal(root.right);
        }


        public static void main(String[] args) {
            Scanner scan = new Scanner(System.in);
            while (scan.hasNextLine()) {
                String str = scan.nextLine();
                TreeNode1 root = createTree(str);
                inOrderTraversal(root);
            }

        }
    }




/*     public class TreeNode{
        char val;
        TreeNode left;
        TreeNode right;
        TreeNode(){

        }
        TreeNode(char val){
            this.val = val;
        }
    }*/


    //根据先序遍历和中序遍历构建二叉树
    public int priIndex;
    public TreeNode buildTree(int[] preorder,int[] inorder){

        return buildTreeChild(preorder,inorder,0,inorder.length-1);
    }
    private TreeNode buildTreeChild(int[] preorder,int[]inorder,int inbegin,int inend){
        //1. 没有左树或没有右树
        if(inbegin>inend){
            return null;
        }
        //2.创建根节点
        TreeNode root = new TreeNode(preorder[priIndex]);
        //3.从中序遍历当中 找到根节点所在的下标
        int rootIndex = findIndex(preorder,inbegin,inend,preorder[priIndex]);
        if(rootIndex == -1){
            return null;
        }
        priIndex++;
        //4. 创建左子树 和 右子树
        root.left = buildTreeChild(preorder,inorder,inbegin,rootIndex-1);
        root.right = buildTreeChild(preorder,inorder,rootIndex-1,inend);
        return root;

    }
    private int findIndex(int[] inorder,int inbegin,int inend,int key){
        for(int i = inbegin;i<= inend;i++){
            if(inorder[i] == key){
                return 0;
            }
        }
        return -1;
    }

    //给你二叉树的根节点 root ，请你采用前序遍历的方式
    // 将二叉树转化为一个由括号和整数组成的字符串，返回构造出的字符串。
    public String tree2str(TreeNode root) {
         StringBuilder stringBuilder = new StringBuilder();
         tree2strChild(root,stringBuilder);
         return stringBuilder.toString();
    }
    public void tree2strChild(TreeNode root,StringBuilder stringBuilder){
        if(root == null){
            return;
        }
        stringBuilder.append(root.val);
        if(root.left != null){
            stringBuilder.append('(');
            tree2strChild(root.left,stringBuilder);
            stringBuilder.append(')');
        }else{
            if(root.right == null ){
                return;
            }else{
                stringBuilder.append("()");
            }

        }
        if(root.right != null){
            stringBuilder.append('(');
            tree2strChild(root.right,stringBuilder);
            stringBuilder.append(')');
        }else{
            if(root.right == null){
                return;
            }else{
                stringBuilder.append("()");
            }
        }

    }

    //二叉树的非递归前序遍历
    public void preOrderNor(TreeNode root) {
        if (root == null) {
            return;
        }
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        TreeNode top = null;
        while (cur != null || !stack.isEmpty()) {
            while (cur != null) {
                stack.push(cur);
                System.out.print(cur.val + " ");
                cur = cur.left;
            }
            top = stack.pop();
            cur = top.right;
        }
    }
    //二叉树的非递归中序遍历
    public void inOrderNor(TreeNode root) {
        if (root == null) {
            return;
        }
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        TreeNode top = null;
        while (cur != null || !stack.isEmpty()) {
            while (cur != null) {
                stack.push(cur);
                cur = cur.left;
            }
            top = stack.pop();
            System.out.print(top.val + " ");
            cur = top.right;
        }
    }

    //二叉树的非递归后序遍历
    public void postOrderNor(TreeNode root) {
        if (root == null) {
            return;
        }
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        TreeNode top = null;
        TreeNode prev = null;
        while (cur != null || !stack.isEmpty()) {
            while (cur != null) {
                stack.push(cur);
                cur = cur.left;
            }
            top = stack.peek();
            if(top.right == null || top.right == prev){
                stack.pop();
                System.out.print(top.val +" ");
                prev = top;
            }else{
                cur = top.right;
            }
        }
    }






}






